
public class WalkingCoord {
	private static WalkingCoord walkingCoord;
	private static Coord c;
	private static int currentState = 0;
	
	private static final int INITIALSTATE  	= 0;
	private static final int EAST     		= 1;
	private static final int NORTH   		= 2;
	private static final int WEST       	= 3;
	private static final int SOUTH  		= 4;
	
	private WalkingCoord() {
		c = new Coord(0,0);
	}
	
	public static int currentState() {
		return currentState;
	}
	
	public static void intialize() {
		if (walkingCoord == null) walkingCoord = new WalkingCoord();
	}
	
	public static void set(Coord newC) {
		c.x = newC.x;
		c.y = newC.y;
		currentState = 0;
	}
	
	public static void set(int x, int y) {
		c.x = x;
		c.y = y;
		currentState = 0;
	}
	
	public static Coord getCurrent() {
		return c;
	}
	
	public static Coord getNext() {
		switch(currentState) {
		case INITIALSTATE : 
			c.x += 1; break;
		case EAST : 
			c.x -= 1;
			c.y -= 1; break;
		case NORTH : 
			c.x -= 1;
			c.y += 1; break;
		case WEST : 
			c.x += 1;
			c.y += 1; break;
		case SOUTH : 
			c.y -= 1; 
			currentState = -1;
			break;
		default: 
			System.err.println("Error: Illegal state in WalkingCoord.");
		}
		currentState++;
		return c;
	}
	
	public static Coord getSouth() {
		switch(currentState) {
		case INITIALSTATE : 
			c.y += 1; break;
		case EAST : 
			c.x -= 1;
			c.y += 1; break;
		case NORTH : 
			c.y += 2; break;
		case WEST : 
			c.x += 1;
			c.y += 1; break;
		case SOUTH : 
			//System.err.println("Warning: getSouth called in SOUTH state");
			break;
		default: 
			System.err.println("Error: Illegal state in WalkingCoord.");
		}
		currentState = SOUTH;
		return c;
	}
	
	public static Coord getNorth() {
		switch(currentState) {
		case INITIALSTATE : 
			c.y -= 1; break;
		case EAST : 
			c.x -= 1;
			c.y -= 1; break;
		case NORTH : 
			System.err.println("Warning: getNorth called in NORTH state");
			break;
		case WEST : 
			c.x += 1;
			c.y -= 1; break;
		case SOUTH : 
			c.y -= 2; break;
		default: 
			System.err.println("Error: Illegal state in WalkingCoord.");
		}
		currentState = NORTH;
		return c;
	}
	
	public static Coord getEast() {
		switch(currentState) {
		case INITIALSTATE : 
			c.x += 1; break;
		case EAST : 
			System.err.println("Warning: getEast called in EAST state");
			break;
		case NORTH : 
			c.x += 1;
			c.y += 1; break;
		case WEST : 
			c.x += 2; break;
		case SOUTH : 
			c.x += 1;
			c.y -= 1; break;
		default: 
			System.err.println("Error: Illegal state in WalkingCoord.");
		}
		currentState = EAST;
		return c;
	}
	
	public static Coord getWest() {
		switch(currentState) {
		case INITIALSTATE : 
			c.x -= 1; break;
		case EAST : 
			c.x -= 2; break;			
		case NORTH : 
			c.x -= 1;
			c.y += 1; break;
		case WEST : 
			System.err.println("Warning: getWest called in WEST state");
		case SOUTH : 
			c.x -= 1;
			c.y -= 1; break;
		default: 
			System.err.println("Error: Illegal state in WalkingCoord.");
		}
		currentState = WEST;
		return c;
	}
	
	public static Coord getInitial() {
		getSouth();
		getNext();
		return c;
	}
	
	public static Coord getFromDirection(char direction) {
		switch(direction) {
		case 'u':
		case 'U':
			return getNorth();
		case 'd':
		case 'D': 
			return getSouth();
		case 'r':
		case 'R': 
			return getEast();
		case 'l':
		case 'L': 
			return getWest();
		default:
			System.err.println("Error: Illegal direction in WalkingCoord.");
			return null;
		}		
	}
	
	public static Coord getFromDirectionRev(char direction) {
		switch(direction) {
		case 'd':
		case 'D':
			return getNorth();
		case 'u':
		case 'U': 
			return getSouth();
		case 'l':
		case 'L': 
			return getEast();
		case 'r':
		case 'R': 
			return getWest();
		default:
			System.err.println("Error: Illegal direction in WalkingCoord.");
			return null;
		}		
	}
	
	public static void setCurrentCoordToInitial() {
		currentState = 0;
	}
}
